import {createStore} from 'vuex'
import {getToken,getUserName,setUserToken,clearUserToken} from "@/plugins/cookie";

export default createStore({
    state: {
        // username: localStorage.getItem('username'),
        // token: localStorage.getItem('token'),
        username:getUserName(),
        token:getToken(),
        car: localStorage.getItem('carNum') || 0
    },
    getters: {},
    mutations: {
        login(state, {username, token}) {
            state.username = username
            state.token = token
            // //保存在localStorage中
            // localStorage.setItem('username', username);
            // localStorage.setItem('token', token);
            setUserToken(username,token)
        },
        logout(state) {
            state.username = ""
            state.token = ""
            // localStorage.removeItem("username")
            // localStorage.removeItem("token")
            clearUserToken()
        },
        addCar(state){
            state.car = parseInt(state.car) + 1;
            localStorage.setItem("carNum",state.car)
        }

    },
    actions: {},
    modules: {}
})
